// 统一菜单配置与构建方法
// 角色：1 学生 2 教师 3 管理员
// permission 对应位：dept, major, class, student, teacher, course, selection, admin

export const RAW_MENUS = [
  { group: '基础教学', path: '/course', label: '课程管理', icon: 'el-icon-notebook-2', permission: 'course', roles: [1,2,3] },
  { group: '基础教学', path: '/course-selection', label: '选课管理', icon: 'el-icon-edit', permission: 'selection', roles: [1,3], badge: (ctx)=> ctx.userType===1? 'S' : '' },
  // 学生自助：显式“选修课程”入口（与 /course-selection 区分，方便学生快速找到）
  { group: '基础教学', path: '/elective-courses', label: '选修课程', icon: 'el-icon-reading', roles: [1], when: ctx => ctx.userType === 1 },
  { group: '人员组织', path: '/class', label: '班级管理', icon: 'el-icon-user', permission: 'class', roles: [2,3] },
  { group: '人员组织', path: '/student', label: '学生管理', icon: 'el-icon-user-solid', permission: 'student', roles: [2,3] },
  { group: '人员组织', path: '/teacher', label: '教师管理', icon: 'el-icon-s-custom', permission: 'teacher', roles: [3] },
  { group: '结构设置', path: '/department', label: '系管理', icon: 'el-icon-office-building', permission: 'dept', roles: [3] },
  { group: '结构设置', path: '/major', label: '专业管理', icon: 'el-icon-collection', permission: 'major', roles: [3] },
  { group: '系统管理', path: '/admin', label: '管理员管理', icon: 'el-icon-setting', permission: 'admin', roles: [3] }
]

export const HOME_TITLE_MAP = {
  1: '学生首页',
  2: '教师首页',
  3: '管理员首页'
}

// 构建菜单：支持超级管理员放行、管理员空权限兜底、角色过滤、权限过滤、动态 when 函数
export function buildMenus (ctx) {
  const { userType, permissions, isSuperAdmin, adminEmptyFallback } = ctx
  return RAW_MENUS.filter(item => {
    if (item.roles && !item.roles.includes(userType)) return false
    if (isSuperAdmin || adminEmptyFallback) return true
    if (item.permission && !permissions.includes(item.permission)) return false
    if (typeof item.when === 'function' && !item.when(ctx)) return false
    return true
  })
}

// 预留：可在此添加运行期动态扩展菜单的钩子
export function extendMenus (extraMenus = []) {
  extraMenus.forEach(m => RAW_MENUS.push(m))
}
